const studentModel = require("../model/studentModel")

module.exports = {
    login({ studentname, password }) {
        // console.log(studentname, password)
        return studentModel.findOne({ studentname, password })
    },
    addList(data) {
        // console.log(data)
        const students = data.list.map(item => ({
            studentname: item.studentname,
            class: data.class,
            score: {},
            password: "123"
        }))

        // console.log(students)
        return studentModel.create(students)
    },
    getList() {
        return studentModel.find().populate("class")
    },
    update(id, { key, value }) {
        return studentModel.findOneAndUpdate({ _id: id }, {
            $set: {
                ["score." + key]: value
            }
        })
    },
    upload(id, file, user) {
        if (file) {
            user.avatarUrl = `uploads/${file.filename}`
            // console.log("图片更新了", user)
        }
        user.studentname = user.username
        return studentModel.findByIdAndUpdate({ _id: id }, user, { returnDocument: 'after' })
    },
    delete(data) {
        console.log(data.id)
        return studentModel.findOneAndDelete({ _id: data.id })
    }
}